package com.mz.ozy.activity.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mz.ozy.R;
import com.mz.ozy.activity.shop.ShopDetailActivity;
import com.mz.ozy.adapter.PopularAdapter;
import com.mz.ozy.adapter.PopularHAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.PopularBean;
import com.mz.ozy.mvp.presenter.PopularPresenter;
import com.mz.ozy.mvp.view.PopularContract;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author lifeStyle on 2020/4/21 0021.
 */
public class PopularActivity extends BaseActivity<PopularContract.View, PopularPresenter> implements PopularContract.View {
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.rv_goods_list)
    RecyclerView rvGoodsList;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    private int pageNo = 1;
    private int tag = 0;
    private List<PopularBean.ActivityListBean> mHBeanList = new ArrayList<>();
    private List<PopularBean.PageBean.PageItemsBean> mTBeanList = new ArrayList<>();
    private PopularAdapter popularAdapter;
    private PopularHAdapter mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popular);
        ButterKnife.bind(this);
        initTitle();
        final String title = getIntent().getStringExtra("title");
        tvCenterTitle.setText(title);
        initView();
    }

    private void initView() {
        recyclerview.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new PopularHAdapter(mHBeanList);
        recyclerview.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                PopularBean.ActivityListBean dataa = mAdapter.getItem(position);
                Intent intent = new Intent(PopularActivity.this, PopularDetailActivity.class);
                intent.putExtra("id", dataa.getId() + "");
                intent.putExtra("name", dataa.getName() + "");
                startActivity(intent);
            }
        });

        rvGoodsList.setLayoutManager(new GridLayoutManager(this, 2));
        rvGoodsList.setNestedScrollingEnabled(false);
        popularAdapter = new PopularAdapter(mTBeanList);
        rvGoodsList.setAdapter(popularAdapter);
        popularAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                PopularBean.PageBean.PageItemsBean dataa = popularAdapter.getItem(position);
                switch (view.getId()) {
                    case R.id.ll_container: {
                        Intent intent = new Intent(PopularActivity.this, ShopDetailActivity.class);
                        intent.putExtra("goodsId", dataa.getPid() + "");
                        intent.putExtra("tag", dataa.getProducttype());
                        startActivity(intent);
                        break;
                    }
                    case R.id.tv_goods_pic: {
                        Intent intent = new Intent(PopularActivity.this, ShopDetailActivity.class);
                        intent.putExtra("goodsId", dataa.getPid() + "");
                        intent.putExtra("show", 1);
                        intent.putExtra("tag", dataa.getProducttype());

                        startActivity(intent);
                        break;
                    }
                }
            }
        });
        srlRefresh.setEnableAutoLoadmore(true);
        srlRefresh.autoRefresh();
        srlRefresh.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                tag = 1;
                pageNo = pageNo + 1;
                mPresenter.getInfo(PopularActivity.this, pageNo + "");
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                if (mPresenter == null) return;
                tag = 0;
                pageNo = 1;
                mPresenter.getInfo(PopularActivity.this, pageNo + "");
            }
        });
    }

    @Override
    public void getInfo(PopularBean bean) {
        if (tag == 0) {
            srlRefresh.setLoadmoreFinished(false);
            srlRefresh.finishRefresh();
            mTBeanList.clear();
            mHBeanList.clear();
            mHBeanList.addAll(bean.getActivity_list());
            mAdapter.notifyDataSetChanged();
            if (bean.getPage().getPageItems().size() == 0) {
                llNoData.setVisibility(View.VISIBLE);
            } else {
                llNoData.setVisibility(View.GONE);
            }
        } else if (tag == 1) {
            srlRefresh.finishLoadmore();
        }
        mTBeanList.addAll(bean.getPage().getPageItems());
        popularAdapter.notifyDataSetChanged();
        if (bean.getPage().getPageItems().size() == 0 || bean.getPage().getPageSize() * bean.getPage().getPageNumber() >= bean.getPage().getPageTotle()) {
            srlRefresh.setLoadmoreFinished(true);//将不会再次触发加载更多事件
        }
    }

    @Override
    public void getInfoDetailSuccess(PopularBean homeMerBean) {

    }
}
